﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace sendfile_server
{
    public class SFClientConnection : SFConnection
    {
        public event EventHandler OnConnecting;
        public event EventHandler OnConnected;

        /// <summary>
        /// normally connection in client
        /// </summary>
        /// <param name="_hostname"></param>
        /// <param name="_port"></param>
        public void Connect(string _hostname, int _port)
        {
            m_TcpClient = new TcpClient();
            m_TcpClient.NoDelay = true;
            m_TcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
            m_TcpClient.Connect(_hostname, _port);
            if (m_TcpClient.Connected)
                m_IsConnected = true;

            if ( this.OnConnected != null)
            {
                OnConnected(this, EventArgs.Empty);
            }

            //if (m_ReadThread == null)
            //    m_ReadThread = new ReadThread(this);
            //if (!m_ReadThread.IsStarted)
            //    m_ReadThread.Start();

            if (m_IdleThread == null)
                m_IdleThread = new IdleThread(this);
            if (!m_IdleThread.IsStarted)
                m_IdleThread.Start();
        }
    }

}
